package com.workman.service.impl;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.workman.bean.AuthorizationUser;
import com.workman.common.util.DateUtil;
import com.workman.dao.mapper.UserMapper;
import com.workman.model.User;
import com.workman.service.UserService;
import com.workman.dao.UserDao;
import org.apache.commons.lang3.time.DateUtils;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.convert.EntityWriter;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: gaochao
 * @time: 2020/11/21 12:14
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService
{

    @Autowired
    private SqlSessionTemplate sqlSessionTemplate;
    @Autowired
    private UserDao userDao;
    @Autowired
    private UserMapper userMapper;



    @Override
    public Map<String,String> selectUserByName(String userNm) {
        return  userDao.selectUserByName(userNm);
    }

    @Override
    public IPage<User> selectUserByPage(Page page, QueryWrapper queryWrapper) {
        IPage<User> iPage= userMapper.selectPage(page, queryWrapper);
        return iPage;
    }

    @Override
    public void insertUser(User user) {
        user.setCreateTime(DateUtil.currentDateTime());
        userMapper.insert(user);

    }

    @Override
    public void updateUserById(User user) {
//        UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
//        userUpdateWrapper.eq("id", user.getId());

        userMapper.updateById(user);
    }

    @Override
    public void updatePwdById(User user) {

    }

    @Override
    public void deleteUserById(User user) {

        userMapper.deleteById(user.getId());
    }

    @Override
    public User selectUserByLoginName(String loginName) {
        QueryWrapper<User> wrapper = new QueryWrapper(new User());
        wrapper.eq("login_name", loginName);
        User user = userMapper.selectOne(wrapper);
        return user;
    }

    @Override
    public User selectUserById(Long id) {
      return userMapper.selectById(id);
    }

    @Override
    public AuthorizationUser getAuthorizationInfo(String loginName) {
        User user = this.selectUserByLoginName(loginName);
        if (user==null){
            return null;
        }
        AuthorizationUser authorizationUser = new AuthorizationUser();
        BeanUtils.copyProperties(user, authorizationUser);


        return authorizationUser;
    }
}
